﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Hi_Baidu.Session
{
    /// <summary>
    ///     会话基类
    /// </summary>
    public abstract class Session : IMetaData, ISession
    {
        /// <summary>
        ///     提供了网络会话的基础支持
        /// </summary>
        /// <param name="Key">会话唯一表示</param>
        /// <param name="SessionType">会话类别</param>
        /// <param name="EndTime">胡话结束时间</param>
        /// <param name="SessionUser">会话所属用户</param>
        public Session(String Key, SessionHelper.SessionType SessionType, DateTime EndTime, User.User SessionUser)
        {
            this.key = Key;
            this.sessionType = SessionType;
            this.startTime = DateTime.Now;
            this.endTime = EndTime;
            this.exprid = false;
            this.sessionUser = SessionUser;
            this.BuildCode();
        }

        private DateTime startTime;
        private DateTime endTime;
        private bool exprid;
        private User.User sessionUser;
        private SessionHelper.SessionType sessionType;
        private String sessionCode;

        /// <summary>
        ///    会话初始时间
        /// </summary>
        public DateTime StartTime
        {
            get { return startTime; }
            set { startTime = value; }
        }
        /// <summary>
        ///     会话结束时间
        /// </summary>
        public DateTime EndTime
        {
            get { return endTime; }
            set { endTime = value; }
        }
        /// <summary>
        ///     会话过期标志位 [过期 Ture/ 未过期 False]
        /// </summary>
        public bool Exprid
        {
            get { return exprid; }
            set { exprid = value; }
        }
        /// <summary>
        ///     会话所属用户
        /// </summary>
        public User.User SessionUser
        {
            get { return sessionUser; }
            set { sessionUser = value; }
        }
        /// <summary>
        ///     会话类别
        /// </summary>
        public SessionHelper.SessionType SessionType
        {
            get { return sessionType; }
            set { sessionType = value; }
        }
        /// <summary>
        ///     会话码
        /// </summary>
        public string SessionCode
        {
            get { return sessionCode; }
        }

        #region IMetaData 成员

        private String key;
        public string Key
        {
            get
            {
                return key;
            }
            set
            {
                key = value;
            }
        }

        #endregion

        #region ISession 成员

        public DateTime GetStartTime()
        {
            return this.startTime;
        }

        public DateTime GetEndTime()
        {
            return this.endTime;
        }

        public bool GetExprid()
        {
            return this.exprid;
        }

        public Hi_Baidu.User.User GetUser()
        {
            return this.sessionUser;
        }

        public abstract void Dispose();

        public new SessionHelper.SessionType GetType()
        {
            return this.sessionType;
        }

        public void BuildCode()
        {
            this.sessionCode =
                EncryptHashHelper.HashString(this.startTime.ToString() + "*(ENCRYPT)*" + this.sessionUser.Username);
        }

        public string GetSessioCode()
        {
            return this.sessionCode;
        }

        #endregion

    }
}
